﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using The_Square_Saga.Objects.Interface;
using The_Square_Saga.Utils;
using The_Square_Saga.Levels.Editor;

namespace The_Square_Saga.Objects
{
    public enum DanteStates
    {
        running,
        attacking,
        stand,
        jumping,
        falling
    }

    class Dante : AnimatedGameEntity, IGameEntity
    {
        private DanteStates currentState;

        public Dante(Texture2D image, Vector2 position, Vector2 velocity)
            :base(image, position )
        {
            this.currentState = DanteStates.stand;
            this.direction = Vector2.Zero;
            this.configAnimations();
        }

        public void configAnimations()
        {
            Animation danteStand = new Animation("stand",new Vector2(37, 68), 250, 0, 0, 4, Axis.X);
            Animation danteRunnig = new Animation("running",new Vector2(51, 68), 100, 0, 1, 7, Axis.X);
            Animation danteJumping = new Animation("jumping",new Vector2(51, 68), 250, 0, 2, 2, Axis.X);
            Animation danteFalling = new Animation("falling",new Vector2(51, 68), 250, 3, 2, 2, Axis.X);
            Animation danteAttacking = new Animation("attacking",new Vector2(51, 68), 250, 0, 0, 4, Axis.X);

            this.addAnimation(danteStand);
            this.addAnimation(danteRunnig);
            this.addAnimation(danteJumping);
            this.addAnimation(danteFalling);
            this.addAnimation(danteAttacking);

            this.setCurrentAnimation("stand");
        }
        #region Input

        private Vector2 getDirection()
        {
            Vector2 d = Vector2.Zero;

            if (Game1.keyboardState.IsKeyDown(Keys.Right))
            {
                deFlipHorizontally();
                d.X = 1;

            }
            else if (Game1.keyboardState.IsKeyDown(Keys.Left))
            {
                flipHorizontally();
                d.X = -1;
            }

            if (Game1.keyboardState.IsKeyDown(Keys.Up))
            {
                d.Y = -1;
            }

            return d;
        }

        private void handleInput()
        {
            this.Direction = getDirection();

            if (direction.X != 0)
            {
                if (currentState == DanteStates.jumping) return;

                currentState = DanteStates.running;
                this.setCurrentAnimation("running");
            }

            if (direction.Y != 0)
            {
                currentState = DanteStates.jumping;
                this.setCurrentAnimation("jumping");
            }

            if (direction == Vector2.Zero)
            {
                this.setCurrentAnimation("stand");
            }

        }
        #endregion
       
        public override void update(GameTime gameTime)
        {
            handleInput();
           
            this.ApplyMovement();
            
            base.update(gameTime);
        }
    }
}
